package com.own.component.util.excel.util.write.method;

import com.own.component.util.excel.model.Coordinate;
import com.own.component.util.excel.util.write.record.ExcelRecord;
import com.own.component.util.excel.util.write.style.css.ExcelCssAttribute;

import java.util.ArrayList;

/**
 * BaseCssMethod
 *
 * @author chenxueli
 * @date 2022-06-15 03:14:15
 */
public interface BaseCssMethod extends BaseValueMethod {

    /**
     * 追加css样式信息
     *
     * @param attributes 样式属性信息
     * @return excel
     */
    default ExcelRecord css(ExcelCssAttribute... attributes) {
        var sheet = sheet();
        return css(sheet.getX(), sheet.getY(), true, attributes);
    }

    /**
     * 追加css样式信息
     *
     * @param x          x坐标
     * @param y          y坐标
     * @param attributes 样式属性信息
     * @return excel
     */
    default ExcelRecord css(int x, int y, ExcelCssAttribute... attributes) {
        return css(x, y, true, attributes);
    }

    /**
     * 追加css样式信息
     *
     * @param x          x坐标
     * @param y          y坐标
     * @param refresh    是否刷新坐标
     * @param attributes 样式属性信息
     * @return excel
     */
    default ExcelRecord css(int x, int y, boolean refresh, ExcelCssAttribute... attributes) {
        var sheet = sheet();
        var writeMap = sheet.getWriteMap();
        var list = sheet.getList();
        var key = Coordinate.buildKey(x, y);
        if (writeMap.containsKey(key)) {
            if (refresh) {
                // 刷新当前的最值
                sheet.setCoordinate(x, y);
            }
            var item = list.get(writeMap.get(key));
            item.addCss(attributes);
        } else {
            setValue(x, y, "", null, false);
            css(x, y, refresh, attributes);
        }
        return excel();
    }

    /**
     * 清除样式信息
     *
     * @return excel
     */
    default ExcelRecord clearCss() {
        var sheet = sheet();
        return clearCss(sheet.getX(), sheet.getY());
    }

    /**
     * 清除样式信息
     *
     * @param x x坐标
     * @param y y坐标
     * @return excel
     */
    default ExcelRecord clearCss(int x, int y) {
        var sheet = sheet();
        var writeMap = sheet.getWriteMap();
        var list = sheet.getList();
        var key = Coordinate.buildKey(x, y);
        if (writeMap.containsKey(key)) {
            list.get(writeMap.get(key)).setCss(new ArrayList<>());
        }
        return excel();
    }

}
